import java.util.Arrays;

/**
 * Description:
 * Date: 2025-10-05
 * Time: 21:31
 */
public class Test {

    public static void main1(String[] args) {
        //二维数组的定义:是一个特殊的一维数组，即元素是一个一维数组
        //二维数组的每一个元素类型都是一个一维数组
        int[][] array = {{1,2,3},{1,2,3}};
        int[][] array1 = new int[][]{{1,2,3},{1,2,3}};
        int[][] array2 = new int[2][3];
        System.out.println(Arrays.toString(array));//[I@4eec7777, [I@3b07d329] 打印出来的是一个一维数组的地址
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));

        //java中二位数组可以只定义行，而不定义列。称为不规则二位数组，而元素内存的是null,不能借助这些元素去调用
        int[][] array3 = new int[2][];
        System.out.println(Arrays.toString(array3));
    }

    public static void main2(String[] args) {
        //二维数组的访问
        int[][] array = {{1,2,3},{4,5,6}};

        for (int i = 0; i < array.length; i++) { //array.length 是访问行数
            for (int j = 0; j < array[i].length; j++) {//访问当前下表中存放的数组元素的长度
                System.out.print(array[i][j] + " ");
            }
        }
    }

    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    //改变原有数组元素的值
    public static void main3(String[] args) {
        int[] array = {1,2,3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }

    //奇数位于偶数之前
    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int n = array.length;
        int left = 0;
        int right = n- 1;
        while (left < right){
            if(array[left] % 2 == 0){
                int tmp = array[left];
                array[left] = array[right];
                array[right] = tmp;
                right--;
            }else {
                left++;
            }
        }
        System.out.println(Arrays.toString(array));
    }

    //两数之和
    public static void main5(String[] args) {
        //假如整数数组是有序的
        int[] nums = {2,7,11,15};
        int target = 17;
        int n = nums.length;
        int right = n - 1;
        int left = 0;
        while (left < right){
            //过滤掉比target还大的整数
            while (left < right && nums[right] > target){
                right--;
            }
            if(nums[left] + nums[right] > target){
                right--;
            }else if (nums[left] + nums[right] < target){
                left++;
            }else{
                System.out.println("[" + left + "," + right + "]");
                break;
            }
        }
    }

    //只出现一次的数字
    public static void main6(String[] args) {
        int[] array = {4,1,2,1,2};
        int tar = 0;
        for (int i = 0; i < array.length; i++) {
            tar ^= array[i];
        }
        System.out.println(tar);
    }

    //多数元素
    public static void main7(String[] args) {
        //数组中出现次数超过一半的元素，排好序后一定是中间位置的的数字
        int[] array = {2,2,1,1,1,2,2,};
        Arrays.sort(array);
        System.out.println(array[array.length / 2]);
//        int n = array.length;
//        int[] tar = new int[array.length * 2];
//        for (int i = 0; i < array.length; i++) {
//            tar[array[i]]++;
//        }
//        for (int i = 0; i < tar.length; i++) {
//            if(tar[i] > (n / 2)){
//                System.out.println(i + " ");
//            }
//        }
    }

    //存在连续三个奇数的数组
    public static void main(String[] args) {
        int[] arr = {1,2,3};
        boolean fal = true;
        int n = arr.length;
        if(n >= 3){
            for (int i = 0; i < n - 2; i++) {
                if ((arr[i] % 2 != 0) && (arr[i+1] % 2 != 0) && (arr[i+2] % 2 != 0)){
                    System.out.println(true);
                    fal = false;
                }
            }
        }else {
            System.out.println(false);
        }
        if(fal == true){
            System.out.println(false);
        }

    }


}
